<template>
  <!-- 导入基本数据 -->
  <a-modal
    :visible="importExeclVisible"
    title="种粮大户补贴分户明细表"
    centered
    :closable="false"
    width="80%"
    dialogClass="member-execl-dialog"
    @cancel="importExeclVisible = false"
  >
    <ImportExeclPreview :execlData="execlData" ref="ImportExeclPreview" />
    <section slot="footer">
      <a-button @click="importExeclVisible = false">取消</a-button>
      <a-button
        type="primary"
        :loading="importExeclConfirmLoading"
        @click="handlerConfirmImport"
        >{{ isPreview ? '待上传文件内容已确认' : '确认导入' }}</a-button
      >
    </section>
  </a-modal>
</template>

<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'
import { ImportExecl } from '@/utils/execl'
import { famerServices, IMember } from '@/common/services/famer'
import ImportExeclPreview from './LargeImportExeclPreview.vue'
import { message, Modal } from 'ant-design-vue'
import { largeMemberServices } from '@/common/services/largeMember'
import MemberImportErrors from '../list/MemberImportErrors.vue'
import { BUSINESS_CODE } from '@/api/interceptors'

@Component({
  components: {
    ImportExeclPreview,
  },
})
export default class MemberImportMixin extends Vue {
  @Prop({
    type: Boolean,
  })
  public isPreview!: boolean
  public file!: File
  // 上传execl
  public importExeclVisible = false
  public importExeclConfirmLoading = false
  public execlData: any[] = []
  public handlerUploadExecl() {
    const _self = this
    let fields: Record<string, string> = {
        big_mb_id: '大户编号', // 大户法定名称
        s_a_id: '受理行政区编号',
        s_a_name: '受理行政区', // 行政地区编号
        big_mb_nam: '大户名称', // 大户法定名称
        big_mb_type: '大户类型', // 大户性质
        big_mb_law_id: '法定编码', // 大户法定编号（农户编号或者组织编号）
        big_mb_user: '负责人', // 负责人姓名
        big_mb_tel: '联系电话', // 负责人电话
        big_mb_sfz: '负责人身份证', // 负责人身份证号
        big_mb_addr: '种粮地点', // 种粮地点
        big_mb_bank_user: '开户名', // 开户人姓名
        big_mb_bank_id: '银行账号', // 开户账号
        big_mb_bank_code: '开户行', // 开户行名称
        field1: '承包地', // 承包确权面积
        field2: '增加面积', // 增加面积
        field3: '减少面积', // 减少面积
        plant: '粮食种植面积（亩）', // 水稻
        plant1: '水稻',
        plant2: '玉米',
        plant3: '小麦',
        plant4: '红苕',
        plant5: '马铃薯',
        plant6: '大豆',
        plant7: '绿豆',
        plant8: '豌葫豆',
        plant9: '高粱',
        plant10: '荞麦',
        plant11: '肾豆',
        plant12: '红小豆',
        material: '购买农资花费（元）',
        machine: '农机数量(台)'
      }
    ImportExecl(
      fields,
      (data, map) => {
        // console.log('data', data)
        // const titles = Object.values(data[1])
        console.log(data, map)
        // 提取表头信息
        const [first, second] = data.slice(0, 2)
        let tempFields = Object.assign({}, second, first)

        for (const i in tempFields) {
          let value = tempFields[i];
          tempFields[value] = i;
          delete tempFields[i];
        }

        for (const i in fields) {
          const hasKey = tempFields[fields[i]]
          if (hasKey) {
            fields[i] = hasKey
          }
        }

        return {
          fields: fields,
          data: data.slice(3)
        }
      }
    ).then((_) => {
      this.importExeclVisible = true
      // @ts-ignore
      this.execlData = _.formatedData || []
      // @ts-ignore
      this.file = _.file
    })
  }

  public async handlerConfirmImport() {
    // @ts-ignore
    const data = this.$refs.ImportExeclPreview.execlData
    if (!data.length) {
      message.error('请导入数据')
      return false
    }
    this.importExeclConfirmLoading = true

    // 预览模式下直接返回数据 + 关闭
    if (this.isPreview) {
      this.$emit('previewSuccess', {
        data,
        file: this.file,
      })
      this.importExeclVisible = false
      return
    }

    try {
      const _ = await largeMemberServices.batchInsert({
        data,
        file_nam: this.file.name
      })
      const res = _.data
      if (res.code === BUSINESS_CODE.SUCCESS) {
        Modal.success({
          content: res.data || '数据导入成功',
          centered: true
        })
      } else {
        this.$dialog(MemberImportErrors, {
          messages: res.msg.map(str => ({ content: str }))
        }, {
          title: '异常大户数据列表',
          width: '60%',
          footer: null
        })
      }
    } finally {
      this.importExeclConfirmLoading = false
      this.importExeclVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.member-container {
  display: flex;
  .area-tree {
    width: 300px;
    flex: none;
    background-color: #fff;
    border-right: 1px solid #eee;
    margin-right: 10px;
  }
}
.house-holder /deep/ .ant-card-body {
  max-height: 600px;
  height: auto;
}
.member-list {
  display: flex;
  flex-direction: column;
}
</style>
